Page({
  data: {
    housedId: '',
    repairItemId: '',
    mobile: '',
    description: '',
    appointment: '',
    houseInfo: '',
    repairItemName: '',
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [

    ],
    repairItem: [],
    // fileList: [
    //   { url: '/repair_pkg/static/uploads/attachment.jpg' },
    //   { url: '/repair_pkg/static/uploads/attachment.jpg' },
    // ],
    attachment: [],
  },
  onLoad({ id }) {
    // 获取房屋列表
    this.getHouseList()
    this.getRepairItem()
    if (id) {
      // id存在是修改
      // 1. 修改页面的title
      wx.setNavigationBarTitle({
        title: '修改报修信息'
      });
      // 2. 回填数据
      this.getRepairDetail(id)

    }
  },
  async getRepairDetail(id) {
    // 请求数据接口
    const { data } = await wx.http.get('/repair/' + id)
    // 渲染报修详情
    this.setData({ ...data })
  },
  delImg(e) {
    // file=》当前要删除图片的数据
    const { file } = e.detail
    this.setData({
      attachment: this.data.attachment.filter(item => item.id !== file.id)
    })
  },
  async getHouseList() {
    // 请求数据接口
    const { code, data: houseList } = await wx.http.get('/house')
    // 检测接口返回的结果
    if (code !== 10000) return wx.utils.toast('获取房屋列表失败!')
    // 数据渲染
    this.setData({ houseList })
  },
  async getRepairItem() {
    // 请求数据接口
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    console.log(repairItem);
    // 检测接口返回的数据
    if (code !== 10000) return wx.utils.toast('获取维修项目失败!')
    // 数据渲染
    this.setData({ repairItem })
  },
  afterRead(e) {
    console.log(e.detail)
    // 临时文件
    const { file } = e.detail
    // 调用接口上传至服务器
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: getApp().token,
      },
      success: (res) => {
        // 转换 json 数据
        const data = JSON.parse(res.data)
        // 上传完成需要更新
        const { attachment } = this.data
        attachment.push({ ...data.data })
        // 渲染数据
        this.setData({ attachment })
      },
    })
  },
  selectDate(e) {
    console.log("时间", e.detail);
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatTime(e.detail)
    })
  },
  selectHouse(e) {
    console.log("选中的房屋信息", e);
    const { name: houseInfo, id: houseId } = e.detail
    this.setData({ houseInfo, houseId })
  },
  selectRepairItem(e) {
    console.log("选中的房屋信息", e);
    const { name: repairItemName, id: repairItemId } = e.detail
    this.setData({ repairItemName, repairItemId })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goList() {
    const { houseId, repairItemId, appointment, mobile, description, attachment } = this.data
    if (!houseId) return wx.utils.toast('请选择房屋信息!', "error")
    if (!repairItemId) return wx.utils.toast('请选择维修项目!', "error")
    if (!/^[1][3-8][0-9]{9}$/.test(mobile.trim())) return wx.utils.toast('请填写正确的手机号码!', "error")
    if (!appointment) return wx.utils.toast('请选择预约日期!', "error")
    if (!description.trim()) return wx.utils.toast('请填写问题描述!', "error")
    console.log('校验过')
    // 请求数据接口
    await wx.http.post('/repair', {
      houseId,
      repairItemId,
      appointment,
      mobile,
      description,
      attachment
    })
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
})
